package com.zscat.mallplus.market.controller;

import com.zscat.mallplus.exception.ApiMallPlusException;
import com.zscat.mallplus.oms.entity.OmsCartItem;
import com.zscat.mallplus.oms.service.IOmsCartItemService;
import com.zscat.mallplus.oms.service.IOmsOrderService;
import com.zscat.mallplus.oms.vo.CartProduct;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.vo.CartParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: rwz
 * @Date: 2020/09/18/8:28 下午
 * @Description:
 */

@Slf4j
@RestController
@Api(tags = "MarketCartController", description = "购物车")
@RequestMapping("/api/market/cart")
public class MarketCartController {

    @Resource
    private IOmsOrderService orderService;

    @Resource
    private IUmsMemberService memberService;

    @Resource
    private IOmsCartItemService cartItemService;

    @ApiOperation("添加商品到购物车")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Object addCart(@RequestBody CartParam cartParam) {
        try {
            return orderService.addCart(cartParam);
        } catch (ApiMallPlusException e) {
            return new CommonResult().failed(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @ApiOperation("查询会员购物车列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Object list() {
        UmsMember umsMember = memberService.getNewCurrentMember();
        Map<String, Object> map = new HashMap<>();
        if (umsMember != null && umsMember.getId() != null) {
            List<OmsCartItem> cartItemList = cartItemService.list(umsMember.getId(), null);
            map.put("cartItemList", cartItemList);
            map.put("promoteAmount", 0);
            Integer goodsCount = 0;
            BigDecimal goodsAmount = new BigDecimal(0.00);
            Integer checkedGoodsCount = 0;
            BigDecimal checkedGoodsAmount = new BigDecimal(0.00);
            for (OmsCartItem cart : cartItemList) {
                goodsCount += cart.getQuantity();
                goodsAmount = goodsAmount.add(cart.getPrice().multiply(new BigDecimal(cart.getQuantity())));
                if (cart.getChecked() == 1) {
                    checkedGoodsCount += cart.getQuantity();
                    checkedGoodsAmount = checkedGoodsAmount.add(cart.getPrice().multiply(new BigDecimal(cart.getQuantity())));
                }
            }
            Map<String, Object> cartTotal = new HashMap<>();
            cartTotal.put("goodsCount", goodsCount);
            cartTotal.put("goodsAmount", goodsAmount);
            cartTotal.put("checkedGoodsCount", checkedGoodsCount);
            cartTotal.put("checkedGoodsAmount", checkedGoodsAmount);

            map.put("cartTotal", cartTotal);

            return new CommonResult().success(map);
        }
        return new CommonResult().success(map);
    }

    @ApiOperation("修改购物车商品数量")
    @RequestMapping(value = "updateQuantity", method = RequestMethod.POST)
    public Object updateQuantity(@RequestParam Long id,
                                 @RequestParam Integer quantity) {
        int count = cartItemService.updateQuantity(id, memberService.getNewCurrentMember().getId(), quantity);
        if (count > 0) {
            return new CommonResult().success(count);
        }
        return new CommonResult().failed();
    }

    @ApiOperation("获取购物车商品属性，用于重选商品属性")
    @RequestMapping(value = "getProductAttribute/{productId}", method = RequestMethod.GET)
    public Object getProductAttribute(@PathVariable Long productId) {
        CartProduct cartProduct = cartItemService.getCartProduct(productId);
        return new CommonResult().success(cartProduct);
    }

    @ApiOperation("修改购物车商品的属性")
    @RequestMapping(value = "updateProductAttribute", method = RequestMethod.POST)
    public Object updateProductAttribute(@RequestBody OmsCartItem cartItem) {
        int count = cartItemService.updateAttr(cartItem);
        if (count > 0) {
            return new CommonResult().success(count);
        }
        return new CommonResult().failed();
    }

    @ApiOperation("删除购物车商品")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public Object delete(String cart_id_list) {
        if (StringUtils.isEmpty(cart_id_list)) {
            return new CommonResult().failed("参数为空");
        }
        List<Long> resultList = new ArrayList<>(cart_id_list.split(",").length);
        for (String s : cart_id_list.split(",")) {
            resultList.add(Long.valueOf(s));
        }
        int count = cartItemService.delete(memberService.getNewCurrentMember().getId(), resultList);
        if (count > 0) {
            return new CommonResult().success(list());
        }
        return new CommonResult().failed();
    }

    @ApiOperation("清空购物车")
    @RequestMapping(value = "clear", method = RequestMethod.POST)
    public Object clear() {
        int count = cartItemService.clear(memberService.getNewCurrentMember().getId());
        if (count > 0) {
            return new CommonResult().success(count);
        }
        return new CommonResult().failed();
    }
}
